package com.example.demo1;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

//@RestController    =@Controller + @ResponseBody
@RequestMapping("/res")
@Controller    //返回视图
public class ResponseController {
    @RequestMapping("/index")
    public String index(){
        return "/index.html";
    }

    @ResponseBody   //返回数据
    @RequestMapping("/indexData")
    public String indexData(){
        return "返回数据";
    }

    @ResponseBody
    @RequestMapping("/indexData2")
    public String indexData2(){
        return "<h1>我喜欢冰淇淋</h1>";
    }

    /**
     * 返回json
     * @return
     */
    @ResponseBody
    @RequestMapping("/getMap")
    public HashMap<String ,String> getMap(){
        HashMap<String ,String >map=new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }


    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(404);//状态码不影响界面展示
        return "设置状态码";
    }

    /**
     * 设置返回类型为json
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "setContentType",produces = "application/json")
    public String setContentType(){
        return "{\"ok\":1}";
    }
}
